<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Login as loginModel;

class Login extends Controller
{
	/**
	 * [login 展示登录方法]
	 * @return [view] [渲染视图]
	 */
	public function login(){
		return view('login/login');
	}

	public function loginDo(){
    	//接收所有值
		$loginData = input('post.');
    	//表单验证
		$rules = [
			'user_name' => 'require',
			'user_pwd' => 'require',
		];
		$msg = [
			'user_name.require' => '用户名必须填写',
			'user_pwd.require' => '密码必须填写',
		];
		$validateRes = $this->validate($loginData,$rules,$msg);
		if($validateRes!== true){
			$this->error($validateRes);
		}
		//调用登录方法
		$loginRes = loginModel::loginTest($loginData);

		if($loginRes == null){
			$this->error('用户或密码错误');
		}

          //存储信息
        session('user_id',$loginRes['user_id']);
        session('user_name',$loginRes['user_name']);
		$this->redirect('/index');
	}
	//退出
	public function loginOut(){
		session(null);
		$this->redirect('/login');
	}
}
